<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item prop="type" label="商城专区">
					<el-select v-model="filters.type" clearable placeholder="商城专区" @change="typeChange">
						<el-option v-for="item,index in mallType" :label="item.name" :value="item.id" :key="index"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-select v-model="filters.first" clearable placeholder="一级分类"  @change="firstChange">
						<el-option
			  				v-for="item,index in firstType"
			  	      			:label="item.name"
			  	      			:value="item.id"
								:key="index">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-select v-model="filters.second" clearable placeholder="二级分类">
						<el-option
			  				v-for="item,index in secondType"
			  	      			:label="item.name"
			  	      			:value="item.id"
								:key="index">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getUsers">查找</el-button>
				</el-form-item>
				
				<el-form-item>
					<el-button type="primary" @click="handleAdd" >添加规格</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="users" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="index" width="" label="序号" align="center">
			</el-table-column>
			<el-table-column label="商城专区" align="center">
				<template slot-scope="scope">
					<el-tag type="success" v-if="scope.row.type == 1">鸿福商城</el-tag>
					<el-tag type="primary" v-if="scope.row.type == 2">积分商城</el-tag>
					<el-tag type="gray" v-if="scope.row.type == 3">旅游专区</el-tag>
					<el-tag type="warning" v-if="scope.row.type == 4">返利商城</el-tag>
				</template>
			</el-table-column>
			<el-table-column prop="skuType" label="规格值类型" align="center">
			</el-table-column>
			<el-table-column prop="skuValue" label="规格值" align="center">
			</el-table-column>
			<el-table-column prop="name" label="分类名称" align="center">
			</el-table-column>
			<el-table-column label="操作" min-width="150" align="center">
				<template slot-scope="scope">
					<el-button type="danger" size="small" @click="handleDelValue(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<div class="toolbar" style="display: flex;justify-content: flex-end;">
			<el-pagination
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				:current-page="currentPage"
				:page-sizes="[10, 20, 30, 40]"
				:page-size="size"
				layout="total, sizes, prev, pager, next, jumper"
				:total="total">
			</el-pagination>
		</div>

		<!--新增界面-->
		<el-dialog title="新增" v-model="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="100px" :rules="addFormRules" ref="addForm">
				<el-form-item prop="type" label="商城专区">
					<el-select v-model="addForm.type" clearable placeholder="商城专区" @change="getFirstCategory">
						<el-option v-for="item,index in mallType" :label="item.name" :value="item.id" :key="index"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="一级分类" prop="first">
					<el-select v-model="addForm.first" clearable placeholder="一级分类"  @change="firstChange2">
						<el-option
			  				v-for="item,index in firstType"
			  	      			:label="item.name"
			  	      			:value="item.id"
								:key="index">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="二级分类" prop="second">
					<el-select v-model="addForm.second" clearable placeholder="二级分类">
						<el-option
			  				v-for="item,index in secondType"
			  	      			:label="item.name"
			  	      			:value="item.id"
								:key="index">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="规格类型" prop="skuType">
					<el-input v-model="addForm.skuType" style="text-align:center;" placeholder="输入规格类型，不超过8个字" auto-complete="off" :maxlength="8"></el-input>
				</el-form-item>
				
				<el-form-item label="规格值" prop="skuValue">
					<el-input v-model="addForm.skuValue" style="text-align:center;" placeholder="输入规格值，不超过8个字" auto-complete="off" :maxlength="8"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit('addForm')" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
		
	</section>
</template>

<script>
	import util from '../../common/js/util'
	import { categoryFeatureValueList, addCategoryFeatureValue, deleteCategoryFeatureValue, deleteGoodsCategoryFeature, addGoodsCategoryFeature,categoryFeatureList,goodsCategoryList, getFirstCategory} from '../../api/api';

	export default {
		data() {
			return {
				mallType: [
					{ id: '1', name: "鸿福商城" },
					{ id: '2', name: "积分商城" },
					{ id: '3', name: "旅游专区" },
					{ id: '4', name: "返利商城" },
				],
				filters: {
					type: '',
					first:'',
					second:'',
				},
				users: [],
				firstType:[],
				secondType:[],
				categoryList:[],
				selectFeature:'',
				selectIndex:0,//当前选择的规格索引
				curCategory:1,//当前选择的分类id
				total: 0,
				currentPage: 1,
				size:10,
				listLoading: false,
				sels: [],//列表选中列
				
				
				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
					skuType: [
						{ required: true, message: '请输入规格类型', trigger: 'blur' }
					],
					skuValue: [
						{ required: true, message: '请输入规格值', trigger: 'blur' }
					],
					type: [
						{ required: true, message: '请选择商城专区', trigger: 'blur' }
					],
					first: [
						{ required: true, message: '请选择一级分类', trigger: 'blur' }
					],
					second: [
						{ required: true, message: '请选择二级分类', trigger: 'blur' }
					]
				},
				//新增界面数据
				addForm: {
					type: '',
					name: '',
					skuType:'',
					skuValue:'',
					first:'',
					second:'',
				}
				,
				addValueVisible: false,//新增界面是否显示
			}
		},
		methods: {
		//获取商品规格值列表
			getUsers() {
				if (Number(this.filters.first)==0 && Number(this.filters.second)==0) {
					this.curCategory = '0';
				}else if(Number(this.filters.first)>0 && Number(this.filters.second)==0){
					this.curCategory = this.filters.first;
				}else {
					this.curCategory = this.filters.second;
				}
				let para = {
					pageIndex: this.currentPage,
					pageSize: this.size,
					categoryId:this.curCategory
				};
				if(!this.filters.type == '') {
					para.type = this.filters.type
				}
				categoryFeatureValueList(para).then((res) => {
					if (res.code == 0) {
						this.users = res.data.data;
						this.total = res.data.totalCount
						this.listLoading = false;
					};
				});
			},
			typeChange() {
				if(this.filters.type == '') {
					this.firstType = []
				}
				let para = {}
				if(!this.filters.type == '') {
					para.type = this.filters.type
					getFirstCategory(para).then(res => {
						this.firstType = res.data;
						this.firstType.map(v => {
							v.id = String(v.id)
						})
					})
				}
			},
			//获取一级分类
			getTypeList(id){
				let para = {
					id:id
				}
				goodsCategoryList(para).then((res) => {
					if (res.code==0) {
						if (id == 0) {
							this.firstType = res.data;
							this.firstType.map(v => {
								v.id = String(v.id)
							})
						}else{
							this.secondType = res.data;
							this.secondType.map(v => {
								v.id = String(v.id)
							})
						}
					};
				});
			},
			getFirstCategory() {
				this.firstType = []
				let para = {}
				if(!this.addForm.type == '') {
					para.type = this.addForm.type
					getFirstCategory(para).then(res => {
						this.firstType = res.data;
						this.firstType.map(v => {
							v.id = String(v.id)
						})
					})
				}
				
			},
			//显示新增界面
			showAddValue: function () {
				this.addValueVisible = true;
			},
			//添加规格值
			addFeatureValue:function(){
				this.$refs.addValue.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.addValue);
							para.featureId = this.categoryList[this.selectIndex].id;
							addCategoryFeatureValue(para).then((res) => {
								this.addLoading = false;
								//NProgress.done();
								if (res.code == 0) {
									this.$message({
										message: '添加成功',
										type: 'success'
									});
									// this.$refs['addForm'].resetFields();
									this.addValueVisible = false;
									this.getUsers(para.featureId );
								}else{
									this.$message({
										message: res.message,
										type: 'error'
									});
								}
							});
						});
					}
				});
			},
			//切换一级分类
			firstChange2:function(){
				this.addForm.second = "";
				if(!this.addForm.first == '') {
					this.getTypeList(this.addForm.first);
				} else {
					this.secondType = [];
				}
			},
			firstChange:function(){
				this.filters.second = "";
				if(!this.filters.first == '') {
					this.getTypeList(this.filters.first);
				} else {
					this.secondType = [];
				}
			},
			//删除规格
			handleDel: function () {
				var info =  this.categoryList[this.selectIndex];
				this.$confirm('确认删除规格【'+info.name+'】吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { id: info.id};
					console.log(para);
					deleteGoodsCategoryFeature(para).then((res) => {
						console.log(res);
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getGoodsCategoryFeatureList();
					});
				}).catch(() => {

				});
			},
			//删除规格值
			handleDelValue: function (index, row) {
				this.$confirm('确认删除该规格值吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { id: row.id };
					deleteCategoryFeatureValue(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getUsers(para.id);
					});
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
			},
			//显示新增界面
			handleAdd: function () {
				this.addFormVisible = true;
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.editForm);
							para.categoryId = this.curCategory;
							addGoodsCategoryFeature(para).then((res) => {
								this.editLoading = false;
								//NProgress.done();
								if (res.code == 0) {
									this.$message({
										message: '提交成功',
										type: 'success'
									});
									// this.$refs['editForm'].resetFields();
									this.editFormVisible = false;
									this.getUsers();
								}else{
									this.$message({
										message: res.message,
										type: 'error'
									});
								}
							});
						});
					}
				});
			},
			//新增规格
			addSubmit(addForm) {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
					
						if (Number(this.addForm.first)==0 && Number(this.addForm.second)==0) {
					this.curCategory = '0';
				}else if(Number(this.addForm.first)>0 && Number(this.addForm.second)==0){
					this.curCategory = this.addForm.first;
				}else {
					this.curCategory = this.addForm.second;
				}
					
				 para = Object.assign({}, this.addForm);
				 
				    let para = {
					categoryId:this.curCategory,
					skuType:this.addForm.skuType,
					skuValue:this.addForm.skuValue,
					 type: this.addForm.type
				};
				    console.log(para)
							addGoodsCategoryFeature(para).then((res) => {
								this.addLoading = false;
								if (res.code == 0) {
									this.$message({
										message: '提交成功',
										type: 'success'
									});
									this.addFormVisible = false;
									this.$refs[addForm].resetFields();
									/*this.filters.first = ''
									this.filters.second = ''*/
									
								this.getUsers();
								}else{
									this.$message({
										message: res.message,
										type: 'error'
									});
								}
							});
							
							
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			// 分页详情
			handleSizeChange(val,id) {
				this.size = val;
				this.getUsers(id)
			},
			handleCurrentChange(val,id) {
				this.currentPage = val;
				this.getUsers(id)
			},
			//选择规格
			changeFeatrue(value){
				console.log("切换为："+value);
				// if () {};
				for (var i = 0; i < this.categoryList.length; i++) {
					var info = this.categoryList[i];
					if (info.name == value) {
						this.selectIndex = i;
						this.getUsers(info.id);
						return;
					};
				};
			}
		},
		mounted() {
			this.getUsers(0);
			//this.getTypeList(0);
		}
	}

</script>

<style scoped>

</style>
